package com.example.livereplay.utils;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MybatisPlusGenerator {

    private final static String basePackage = "com.example";
    private final static String baseModuleName = "livereplay";
    private final static String basePath = System.getProperty("user.dir") + "\\src\\main\\java";
//    private final static String resourcesPath = System.getProperty("user.dir") + "\\src\\main\\resources\\mappers\\" + baseModuleName;
    private final static String resourcesPath = System.getProperty("user.dir") + "\\src\\main\\resources\\mappers\\" ;

    private final static String dbUserName = "root";
    private final static String dbPassword = "xsbmxtdb@2024";
    private final static String dbUrl = "jdbc:mysql://47.96.81.43:3306/live_replay?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&transformedBitIsBoolean=true";

    @Test
    public void generate() {
        final List<IFill> fillList = new ArrayList<>();
        final Column createTime = new Column("create_time", FieldFill.INSERT);
        final Column updateTime = new Column("update_time", FieldFill.UPDATE);
        final Column createBy = new Column("create_by", FieldFill.INSERT);
        final Column updateBy = new Column("update_by", FieldFill.UPDATE);
        fillList.add(createTime);
        fillList.add(updateTime);
        fillList.add(createBy);
        fillList.add(updateBy);
        FastAutoGenerator.create(dbUrl, dbUserName, dbPassword)
                .globalConfig(builder -> {
                    builder.author("郑心窕") // 设置作者
                            .enableSpringdoc() // 开启 swagger 模式
                            .outputDir(basePath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(basePackage) // 设置父包名
                            .moduleName(baseModuleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, resourcesPath)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> builder.addInclude("") // 设置需要生成的表名
                        .addTablePrefix("t_", "info_") // 设置过滤表前缀
                        .entityBuilder()
                        .enableLombok()
                        .idType(IdType.ASSIGN_ID)
                        .versionColumnName("version")
                        .logicDeleteColumnName("deleted")
                        .addTableFills(fillList)
                        .mapperBuilder()
                        .enableBaseColumnList()
                        .enableBaseResultMap()
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
                        .controllerBuilder()
                        .enableRestStyle())
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
